package lt.laimis.shop.dao;

import org.alfresco.webservice.types.*;
import org.alfresco.webservice.repository.QueryResult;
import org.alfresco.webservice.util.WebServiceFactory;
import org.alfresco.webservice.util.Constants;
import org.alfresco.webservice.util.AuthenticationUtils;
import org.alfresco.webservice.authentication.AuthenticationServiceLocator;
import org.alfresco.webservice.authentication.AuthenticationFault;

import java.util.List;
import java.util.ArrayList;
import java.rmi.RemoteException;

import lt.laimis.shop.service.node.Node;
import lt.laimis.shop.service.product.Product;
import lt.laimis.shop.service.content.Content;

/**
 * Created by IntelliJ IDEA.
 * User: laimis
 * Date: Jul 29, 2008
 * Time: 7:30:49 PM
 * To change this template use File | Settings | File Templates.
 */

public class DaoNode {

    protected static final Store STORE = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
    protected static final Reference SEX_SHOP_FOLDER = new Reference(STORE, null, "/app:company_home/cm:sex_shop");
    protected static final String REMOTE_ALFRESCO_URL = "http://localhost:8080/alfresco/";
    private String authenticationServiceEndpointAddress;
    private String user;
    private String password;
    private String nodeQName;
    private String productQName;

    private List<Node> listNode = new ArrayList<Node>();
    private List<Product> listProduct = new ArrayList<Product>();
    private List<Content> listContent = new ArrayList<Content>();

    public List<Product> getListProduct() {
        return listProduct;
    }

    public List<Content> getListContent() {
        return listContent;
    }

    public List<Node> getListNode() {
        if (listNode.size() == 0) {
            openSession();
            setListResultSetRow(SEX_SHOP_FOLDER);
            closeSession();
        }
        return listNode;
    }

    public void openSession() {
        AuthenticationServiceLocator aservice = new AuthenticationServiceLocator();
        aservice.setAuthenticationServiceEndpointAddress(getAuthenticationServiceEndpointAddress());
        try {
            AuthenticationUtils.startSession(getUser(), getPassword());
        } catch (AuthenticationFault authenticationFault) {
            authenticationFault.printStackTrace();
        }
    }
  //  http://localhost:8080/alfresco/d/d/workspace/SpacesStore/c6087e1e-62de-11dd-8dc4-b3e0e0344ad6/Picture 325.jpg
    public void closeSession() {
        AuthenticationUtils.endSession();
    }


    public void setListResultSetRow(Reference newRef) {
        ResultSetRow[] resultSetRow;
        try {
            QueryResult node1 = WebServiceFactory.getRepositoryService().queryChildren(newRef);
            ResultSet resultSet = node1.getResultSet();
            resultSetRow = resultSet.getRows();
            if (resultSetRow != null && resultSetRow.length > 0) {
                for (ResultSetRow rsr : resultSetRow) {
                    if (rsr != null) {
                        Object node = extractResultSet(rsr, newRef);
                        if (node != null){
                            if(node.getClass().getName().equals("lt.laimis.shop.service.node.Node")){
                                listNode.add((Node)node);
                            }
                            else if (node.getClass().getName().equals("lt.laimis.shop.service.product.Product")){
                                listProduct.add((Product)node);
                            }
                        else if (node.getClass().getName().equals("lt.laimis.shop.service.content.Content")){
                                listContent.add((Content)node);
                            }

                        }
                        setListResultSetRow(new Reference(STORE, rsr.getNode().getId(), null));
                    }
                }
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }


    public Object extractResultSet(ResultSetRow resultSer, Reference newRef) {
        Node node;
        Product product;
        Content content;
        if (resultSer != null) {
            if (resultSer.getNode().getType().equals(getNodeQName())) {
                node = new Node(resultSer.getNode().getId(),
                        resultSer.getNode().getType(),
                        getNamedValue(resultSer,"{http://www.alfresco.org/model/content/1.0}name"),
                        getNamedValue(resultSer,"{http://www.alfresco.org/model/content/1.0}title"),
                        getParentId(newRef),
                        getNamedValue(resultSer,"{http://www.alfresco.org/model/content/1.0}description"));
               return node;
            } else if (resultSer.getNode().getType().equals(getProductQName())) {
                product = new Product(resultSer.getNode().getId(),
                        resultSer.getNode().getType(),
                        getNamedValue(resultSer,"{http://www.alfresco.org/model/content/1.0}name"),
                        getNamedValue(resultSer,"{http://www.alfresco.org/model/content/1.0}title"),
                        getParentId(newRef),
                        getNamedValue(resultSer,"{http://www.alfresco.org/model/content/1.0}description"),
                        getProdcutPrice(resultSer,"{http://www.alfresco.org/model/content/1.0}price"),
                        getNamedValue(resultSer,"{http://www.alfresco.org/model/content/1.0}note"));
               return product;

            }else if (resultSer.getNode().getType().equals("{http://www.alfresco.org/model/content/1.0}content")) {
                content = new Content(resultSer.getNode().getId(),
                        resultSer.getNode().getType(),
                        getNamedValue(resultSer,"{http://www.alfresco.org/model/content/1.0}name"),
                        getNamedValue(resultSer,"{http://www.alfresco.org/model/content/1.0}title"),
                        getParentId(newRef),
                        getNamedValue(resultSer,"{http://www.alfresco.org/model/content/1.0}description"),
                        getImageURl(resultSer));
           return content;
        }
        }
        return null;
    }

    private int getProdcutPrice(ResultSetRow resultSer, String name) {
        int value = 0;
        if (resultSer != null ){
            for(NamedValue namedValue :resultSer.getColumns()){
               if(namedValue.getName().equals(name)){
                   String string  =  namedValue.getValue();
                   value = Integer.parseInt(string);
                   continue;
               }
            }
        }
      return value;
    }


    public String getParentId(Reference newRef) {
        String parentId = "";
        if (newRef.getUuid() != null) {
            parentId = newRef.getUuid();
        }
        return parentId;
    }

        public String getNamedValue(ResultSetRow resultSer, String name) {
        String value = "";
        if (resultSer != null ){
            for(NamedValue namedValue :resultSer.getColumns()){
               if(namedValue.getName().equals(name)){
                   value = namedValue.getValue();
                   continue;
               }
            }
        }
        return value;
    }

      private String getImageURl(ResultSetRow resultSer) {
        return REMOTE_ALFRESCO_URL + "d/d/workspace/SpacesStore/" + resultSer.getNode().getId() +"/"+ getNamedValue(resultSer,"{http://www.alfresco.org/model/content/1.0}name") ;
    }

    public void setListNode(List<Node> listNode) {
        this.listNode = listNode;
    }

    public String getAuthenticationServiceEndpointAddress() {
        return authenticationServiceEndpointAddress;
    }

    public void setAuthenticationServiceEndpointAddress(String authenticationServiceEndpointAddress) {
        this.authenticationServiceEndpointAddress = authenticationServiceEndpointAddress;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getNodeQName() {
        return nodeQName;
    }

    public void setNodeQName(String nodeQName) {
        this.nodeQName = nodeQName;
    }

    public String getProductQName() {
        return productQName;
    }

    public void setProductQName(String productQName) {
        this.productQName = productQName;
    }
}
